<template>
  <div class="tariffEidt" curpage="gtariffEidt323423432423423">
    <div class="exitExclel">
      <div class="header">
        <div class="headerContent">
          <div class="titleTxt">
    
          {{$t('merak.bath_edit')}}
 
          </div>
          <div class="btnBox">
            
            <el-button plain @click="closeWindow">{{$t('merak.no_bath_edit')}}</el-button>
            <el-button type="primary" @click="confirmSubmit">{{$t("merak.batch_confirm_submit")}}</el-button>
          </div>
        </div>
      </div>
      <div class="editTableDiv" style="" v-if="!headLoading">
        <editTableExcel ref="editTableRef" v-bind="tableConfig"></editTableExcel>
      </div>
    </div>
    <div>
      <cardSatus v-bind="loadingParams" @contunitEdit="contunitEditFun"></cardSatus>
    </div>
 
  </div>
</template>
<script setup lang="ts" name="onlineMoreEditTariff">
import editTableExcel from "@/components/MerakEditTable/components/editTableExcel/indexTwo.vue"; 
import qfInput from "@/components/MerakEditTable/components/a-qfInput/index.vue";
import cardSatus from "@/components/MerakEditTable/components/a-loading/index.vue";
import { ref, reactive, defineProps, onMounted, computed, nextTick } from "vue";

import { getTariffFormApi,getTariffBathEditApi,getTariffBathSaveApi  } from "@/api/modules/goodsArchive/tariff/tariff";
import { ResultEnum } from "@/enums/httpEnum";
import { cos } from "mathjs";
import { getLocationParams } from "@/utils/index";

import {
  moreConfirm,
  confirmWarningMsg,
  successMsg,
  warnMsg,
  failMsg,
  confirmSuccessMsg,
  delConfimMsg
} from "@/utils/messageUtils";
import { type } from "os";

const onlyErr = ref(false);
//接收url参数
const newUrl = new URL(window.location.href);
let typeId= getLocationParams(newUrl.href, "tId");
let ids = getLocationParams(newUrl.href, "ids");
console.log(typeId, "传入参数");
console.log(ids, "传入参数");
const editTableRef = ref(null);
const value2 = ref("");

const loadingParams = reactive({
  isShowProgress: false, //提交loading
  currNumber: 0, //当前提交条数
  allErrNumber: 0, //全部条数
  errorNumber: 0, //编辑完剩余错误条数
  successNumber: 0, //成功条数
  allErrTotal: 0,//所有错误条数 
  allFinishedTotal:0,//所有错误条数
});
const tourShow = ref(false);
let errInfoArr = []; //错误数组
const splitNumber = 10;
let splitIndex = 0;
const confirmSubmit = async () => {
  splitIndex = 0;
  errInfoArr = []; //每次提交都清空
  let postDataArr = editTableRef.value.saveParams();
  console.log(postDataArr);
  let allPostLen = postDataArr.length; //所有提交数组长度
  loadingParams.allErrNumber = allPostLen; //所有长度
  loadingParams.currNumber = 0; //
  loadingParams.isShowProgress = true; //打开提交弹窗

  //每10个拆分形成新数组=
  let splitedArrs = [];
  let size = splitNumber;
  for (let i = 0; i < allPostLen; i += size) {
    let splited = postDataArr.slice(i, i + size);
    splitedArrs.push(splited);
  }

  let splitLen = splitedArrs.length;
  for (let ss = 0; ss < splitLen; ss++) {
    await postDateFun(splitedArrs[ss]).then(errDataInfo => {
      splitIndex = splitIndex + splitNumber;
      loadingParams.currNumber = splitIndex; //当前提交条数

      //   //测试   所有都提交完了
      if (errDataInfo.length && Array.isArray(errDataInfo)) {
        errInfoArr = errInfoArr.concat(errDataInfo);
      }

      if (splitLen - 1 == ss) {
        //最后一组提交成功后

        loadingParams.currNumber = allPostLen; 
        loadingParams.errorNumber = errInfoArr.length; //错误条数
        loadingParams.successNumber = allPostLen - errInfoArr.length; //成功条数
        console.log(loadingParams)
      }
    });
  }


};

const postDateFun = dataParams => {
  //   saveTableGoodsApi({data:dataParams}).then(res=>{

  //   })
  return new Promise((resolve, reject) => {
    getTariffBathSaveApi( {data:dataParams,type_id:typeId})
      .then(res => {
        console.log(res);
        if (res.code == ResultEnum.SUCCESS) {
          resolve(res.data);
        }
      })
      .catch(err => {
        console.log("9999999");
        resolve(dataParams);
      });
 
  });
};

//继续移交
const contunitEditFun = val => {
  loadingParams.isShowProgress = false;

  if (val == "other") {
    //继续编辑其余的数据
    initData();
    return;
  }
let allCont=loadingParams.allErrTotal>100?100:loadingParams.allErrTotal;
  // Object.assign(, errInfoArr);
  if (onlyErr.value) {
    //只显示错误数据,重新请求头部
    getTableFieldsApi({ import_id: import_id, errorColumn: 1 }).then(res => {
      tableConfig.headData = res.data;
      tableConfig.tableData = errInfoArr;
      loadingParams.allErrNumber = errInfoArr.length;
      
      loadingParams.allFinishedTotal=allCont - loadingParams.allErrNumber;//所有成功的条数
      headLoading.value = true;
      nextTick(() => {
        headLoading.value = false;
      });
    });
    return;
  }
   tableConfig.tableData = errInfoArr;
  loadingParams.allErrNumber=errInfoArr.length;
  loadingParams.allFinishedTotal=allCont - loadingParams.allErrNumber;//所有成功的条数
  headLoading.value = true;
  nextTick(() => {
    headLoading.value = false;
  });
};

const onlyErrchange = e => {
  initData();
};
const closeWindow = () => {
  window.close(); 
};

// 行列选中样式  当前选中样式
// 行尾部和删除
const tableConfig = reactive({
  frozenNum:2,//冻结列
  headData: [], //表格头部数据
  tableData: [] //表格内容数据
});

 

const headLoading = ref(false);
const initData = () => { 
  headLoading.value = true;
 
  getTariffFormApi({ type_id: typeId}).then(res => {
    
    tableConfig.headData = res.data;
    getTariffBathEditApi({ ids: ids.split(","), type_id:typeId }).then(res => {
      let newData = res.data;
      
      console.log(newData); 
      loadingParams.allFinishedTotal=0;
      loadingParams.allErrNumber = newData.length;
      tableConfig.tableData = newData;
      loadingParams.allErrTotal =ids.split(",").length;
      headLoading.value = false;
    });
  }); 
};

onMounted(() => {
  initData();
});
</script>

<style scoped lang="scss">
@import "./edit.scss";
</style>
